package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /*
    * 添加购物车
    * */
    @Transactional
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前数据是否已经存在数据库内
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCart);
        if (shoppingCarts!=null && shoppingCarts.size()>0) {
            //已经在数据库则修改信息
            ShoppingCart cart = shoppingCarts.get(0);
            cart.setNumber(cart.getNumber()+1);
            //更新数据
            shoppingCartMapper.update(cart);
        }else {
            //不然则添加信息
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            //判断为菜品还是套餐
            if (dishId!=null){
                //菜品,获取菜品数据
                Dish dish=dishMapper.selsctById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }
            if (shoppingCart.getSetmealId()!=null){
                //套餐,获取套餐数据
                Setmeal setmeal = setmealMapper.selsctById(setmealId);

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将补全数据的信息添加到数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /*
    * 查看购物车
    * */
    @Override
    public List<ShoppingCart> getShoppingCart() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //将id封装到shoppingCart对象当中
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        // 查询购物车数据并返回
        return  shoppingCartMapper.selectList(cart);
    }

    /*
    * 删除购物车中的一个商品
    * */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //构建一个ShoppingCart对象
        ShoppingCart shoopingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoopingCart);
        shoopingCart.setUserId(userId);
        //根据已有条件到数据库查询对应信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoopingCart);
        if (shoppingCarts!=null && shoppingCarts.size()>0) {
            ShoppingCart cart = shoppingCarts.get(0);
            //判断当前购物车产品数量,为一则删除购物车
            if (cart.getNumber()==1) {
                shoppingCartMapper.deleteById(shoopingCart);
            }
            //大于一则数量-1
            if (cart.getNumber()>1) {
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.update(cart);
            }
        }
    }

    /*
     * 清空购物车中所有内容
     * */
    @Override
    public void clearShoppingCart() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //构建一个ShoppingCart对象
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        shoppingCartMapper.deleteById(cart);
    }
}
